package com.ruoyi.project.info.doctor.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.info.doctor.domain.Doctor;
import com.ruoyi.project.info.doctor.service.IDoctorService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 医生管理Controller
 *
 * @author wzm
 * @date 2022-09-07
 */
@Controller
@RequestMapping("/info/doctor")
public class DoctorController extends BaseController
{
    private String prefix = "info/doctor";

    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private IUserService userService;
    @RequiresPermissions("info:doctor:view")
    @GetMapping()
    public String doctor()
    {
        return prefix + "/doctor";
    }

    /**
     * 查询医生管理列表
     */
    @RequiresPermissions("info:doctor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Doctor doctor)
    {
        startPage();
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        return getDataTable(list);
    }

    /**
     * 导出医生管理列表
     */
    @RequiresPermissions("info:doctor:export")
    @Log(title = "医生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Doctor doctor)
    {
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        ExcelUtil<Doctor> util = new ExcelUtil<Doctor>(Doctor.class);
        return util.exportExcel(list, "医生管理数据");
    }

    /**
     * 新增医生管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存医生管理
     */
    @RequiresPermissions("info:doctor:add")
    @Log(title = "医生管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Doctor doctor)
    {
        doctor.preInsert();
        Doctor doctor1=new Doctor();
        doctor1.setPhone(doctor.getPhone());
        List<Doctor> doctors=doctorService.selectDoctorList(doctor1);
        if(doctors!=null&&doctors.size()>0){
            return AjaxResult.error("手机号已存在！");
        }
        if(StringUtils.isNotEmpty(doctor.getIdcard())){
            doctor1=new Doctor();
            doctor1.setIdcard(doctor.getIdcard());
            doctors=doctorService.selectDoctorList(doctor1);
            if(doctors!=null&&doctors.size()>0){
                return AjaxResult.error("身份证号已存在！");
            }
        }

        return toAjax(doctorService.insertDoctor(doctor));
    }

    /**
     * 修改医生管理
     */
    @RequiresPermissions("info:doctor:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Doctor doctor = doctorService.selectDoctorById(id);
        mmap.put("doctor", doctor);
        return prefix + "/edit";
    }

    /**
     * 修改保存医生管理
     */
    @RequiresPermissions("info:doctor:edit")
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Doctor doctor)
    {
        doctor.preUpdate();
        if(StringUtils.isNotEmpty(doctor.getIdcard())){
            Doctor doctor1=new Doctor();
            doctor1.setIdcard(doctor.getIdcard());
            List<Doctor> doctors=doctorService.selectDoctorList(doctor1);
            if(doctors!=null&&doctors.size()>0){
                boolean isHas=false;
                for(Doctor d:doctors){
                    if(d.getId()!=doctor.getId()){
                        isHas=true;
                        break;
                    }
                }
                if(isHas){
                    return AjaxResult.error("身份证号已存在！");
                }
            }
        }
        return toAjax(doctorService.updateDoctor(doctor));
    }
    @Log(title = "医生管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("info:doctor:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Doctor> util = new ExcelUtil<Doctor>(Doctor.class);
        List<Doctor> userList = util.importExcel(file.getInputStream());
        String message = doctorService.importUser(userList, updateSupport);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("info:doctor:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Doctor> util = new ExcelUtil<Doctor>(Doctor.class);
        return util.importTemplateExcel("医生信息");
    }
    @RequiresPermissions("info:doctor:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Doctor doctor)
    {
        return toAjax(doctorService.changeStatus(doctor));
    }
    /**
     * 删除医生管理
     */
    @RequiresPermissions("info:doctor:remove")
    @Log(title = "医生管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(doctorService.deleteDoctorByIds(ids));
    }
}
